﻿function longinClient(dm,index)
  local n = GetBindWindow(dm)
  if (n > 0) then
    show("解除绑定")
    UnBindWindow(dm)
   --如果存在绑定则解除绑定
  end
  MoveTo(dm, 2, 5)
  show("打开游戏")
  hwnd = FindWindowEx(dm, "", "MapleStory")
  show("游戏开始窗口句柄存在关闭游戏" .. tostring(hwnd))
  if (hwnd ~= 0) then
    show("关闭游戏进程")
    pids = EnumProcess(dm, "GameLauncher.exe")
    show(pids)
    local tab = split(pids, ",")
    for key, value in ipairs(tab) do
      TerminateProcess(dm, value)
      delay(dm,500)
    end
    pids = EnumProcess(dm, "MapleStory.exe")

    tab = split(pids, ",")
    for key, value in ipairs(tab) do
      TerminateProcess(dm, value)
      delay(dm,500)
    end
  end
  runScript(dm,'游戏图标')
  delay(dm,500)
  hwnd = FindWindowEx(dm, "", "MapleStory")
  if(hwnd~=0)
  then
    show("游戏引导已经打开")
    --SetWindowState(dm,hwnd,12)
    delay(dm,1000)
    KeyPress(dm,'tab');
    delay(dm,1000)
    KeyPress(dm,'enter');
    delay(dm,10000)
  end

  for i=1,150 do 
    pids = EnumProcess(dm, "GameLauncher.exe")
    if(pids~='')
    then
      show("窗口存在退出")
      break
    end

    pids = EnumProcess(dm, "MapleStory.exe")
    if(pids=='' or pids==nil)
    then
      show("游戏进程不存在重新开始")
      break
    end

    hwnd = FindWindowEx(dm, "MapleStoryClass", "MapleStory")
    --show("查找窗口句柄" .. tostring(hwnd).."索引："..tostring(index).."账号："..GetItemText(index,1))
    show("等待启动.窗口句柄".. tostring(hwnd))
    delay(dm,1000)  
    if(hwnd~=0)
    then
      ret=BindWindowEx(dm,hwnd)
      show("窗口绑定" .. tostring(ret))
      if(ret==1)
      then
        delay(dm,1000)  
        MoveWindow(dm,hwnd,0,0)
        show("窗口绑定成功")
        break
      end
    end
  end
 
  for i=1,30 do 
    if(runScript(dm,'进入登录'))
    then
      flag=true;
      show("游戏登录成功")
      break
    end
    delay(dm,1000)
  end

show("启动客户端成功开始登录游戏")
if(flag == true)
then
  return true
end
return false

end

--登录
function login(dm,index)
  --loginGame(dm)
  if(longinClient(dm,index))--客户端开启成功
  then--开始登录游戏
   return loginGame(dm,index)
 end
end


--登录游戏
function loginGame(dm,index)

  --测试开始
  -- local n = GetBindWindow(dm)
  -- if (n > 0) then
  --   show("解除绑定")
  --   UnBindWindow(dm)
  --  --如果存在绑定则解除绑定
  -- end
  -- hwnd = FindWindowEx(dm, "MapleStoryClass", "MapleStory")
  -- show("查找窗口句柄" .. tostring(hwnd))
  -- delay(dm,1000)  
  -- if(hwnd~=0)
  -- then
  --   ret=BindWindowEx(dm,hwnd)
  --   show("窗口绑定" .. tostring(ret))
  --   if(ret==1)
  --   then
  --     delay(dm,1000)  
  --     MoveWindow(dm,hwnd,0,0)
  --     show("窗口绑定成功")

  --   end
  -- end
  -- --测试结束
  -- SetWindowState(dm,hwnd,12)
while(true)
do
  show("1")
  if(runScript(dm,'账号在线'))
  then
    --更新列表状态列表状态
  end
  if(runScript(dm,'账号密码错误'))
  then
     --更新列表状态列表状态
  end
  if(runScript(dm,'二密错误'))
  then
     --更新列表状态列表状态
  end

  show("2")
  if(runScript(dm,'进入登录'))--到达登录页面
  then
    show(GetItemText(index,1))
    runScripts(dm,'输入账号',GetItemText(index,1),1)
    delay(dm,1000)
    show(GetItemText(index,2))
    runScripts(dm,'输入密码',GetItemText(index,2),1)
    delay(dm,2000)  
    show("点击登录"..tostring(runScript(dm,'点击登录')))
  end
  --如果为保号
  baohao=GetItemText(index,11)
  show(baohao)
  if(baohao~='' and baohao~=nil)
  then
    --选择保号
    show("3")
    delay(dm,2000)  
    runScripts(dm,'选择账号',baohao,2)--选择保号登录
  else
    delay(dm,2000) 
    runScript(dm,'选中账号')--选择默认第一账号
  end
  delay(dm,2000)
  server = 'loginChoseServer'..GetItemText(index,4)..'.bmp'
  show(server)
  runScripts(dm,"选择区服",'loginChoseServer2.bmp',2)
  delay(dm,2000)
  runScript(dm,'选择线路25')
  --取是否输入二密状态
  delay(dm,2000)
  
  if(runScript(dm,'角色页面'))--到达角色页面且未输入二密
  then
    --选择单一角色方案
    show("进入")
    delay(dm,1000)
    --二密窗口是否存在不存在则按回车键
    if(runScript(dm,'二密窗口')==false)
    then
      KeyPress(dm,'enter');--回车弹出二密窗口
    end
    delay(dm,1000)
    keyWord = GetItemText(index,3);--二密
    show("二密")
    twoCodeImg = "";
    for i = 1, string.len(keyWord) do
      if(i == string.len(keyWord) )
      then
        twoCodeImg = twoCodeImg..string.sub(keyWord,i,i)..".bmp"
      else
        twoCodeImg = twoCodeImg..string.sub(keyWord,i,i)..".bmp|"
      end
    end
    show(twoCodeImg)
    strTwoCods=runScriptExs(dm,"二密查找",twoCodeImg)
    tab = split(strTwoCods, "|")
    i=1
    show('22')
    while(i<=#tab)
    do
      show("开始")
      --分割字符
      tabs = split(tab[i], ",")
      show(tabs[2]..","..tabs[3])
      ClickEx(dm,tonumber(tabs[2]),tonumber(tabs[3])+5,10,10)
      delay(dm, math.random(1500,1800))  
      strXing = runScriptExs(dm,"星号查找","pwdxing.bmp")
      Xings= split(strXing, "|")
      show(strXing)
      if(i~=#Xings)
      then
        i=i-1
      end
      i=i+1
    end
    if(runScript(dm,'二密确认'))
    then
      delay(dm, math.random(5500,6800))  
    end
    --show(strTwoCods)
    --读取二密坐标
    --runScriptE("坐标","类型","动作")
    --runScriptEx("坐标","类型","动作","相似度","偏色","偏移","随机范围")
    --runScriptExs("坐标","数据","类型")
    --runScriptExs1("坐标","数据","类型","相似度","偏色")

  end
  
  if(runScript(dm,'登录成功'))
  then
    show('登录成功')
    return true
    --break;
  end

end

end




function GetItemText(index,itemIndex)--获取界面列表数据
  if(index==-1)
  then
    if(itemIndex == 1)--账号
    then
      return "zgsp46511@daum.net"--索引为-1返回默认值
    end
    if(itemIndex == 2)--密码
    then
      return "zodw147258"--索引为-1返回默认值
    end
    if(itemIndex == 3)--二密
    then
      return "jie2560"
    end
    if(itemIndex == 11)--保号
    then
      return ""--索引为-1返回默认值
    end
    if(itemIndex == 4)--服务器
    then
      return "2"--索引为-1返回默认值
    end
  end
  show("索引:"..tostring(index))

  return GetItemListText(index,itemIndex)
end




function login1(dm)
  if(runScript(dm,'进入登录'))
  then
    show("游戏登录成功")
  end
end


function split(str, reps)
  local resultStrList = {}
  string.gsub(
    str,
    "[^" .. reps .. "]+",
    function(w)
      table.insert(resultStrList, w)
    end
  )
  return resultStrList
end
